package com.zh.gateway.filter;

import com.zh.common.properties.JwtProperties;
import com.zh.common.utils.RedisUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
@RequiredArgsConstructor
public class AuthGlobeFilter implements GlobalFilter, Ordered {
    private final RedisUtil redisUtil;
    private final JwtProperties jwtProperties;

    @Override
    public int getOrder() {
        return 0;
    }

    /**
     * 转发携带请求头
     *
     * @param exchange
     * @param chain
     * @return
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        HttpHeaders headers = request.getHeaders();
        String authToken = headers.getFirst(jwtProperties.getTokenName());

        return chain.filter(exchange);
    }
}